As a service, here’s the data loading routine again:

library(dplyr)
library(haven)

gp_covid <- 
  read_sav(
    "./data/ZA5667_v1-1-0.sav"
  ) %>% 
  sjlabelled::set_na(na = c(-1:-99, 97:98))

As we will discuss in more detail in the session on Exploratory Data Analysis, data exploration is not only about creating numbers and summary statistics. Sometimes a good plot can reveal more insights than a whole data frame filled with numbers (especially to the human eye). In this exercise, we use what we’ve just learned about plots with ggplot2. This time we are going to use the Gapminder data on GDP per capita.

Hence, we we need to first load the Gapminder GDP data from the CSV file and convert it to long format (as in the data wrangling session).

library(dplyr)
library(tidyr)

gapminder_ggplot_input <-
  readr::read_csv("./data/gdppercapita_us_inflation_adjusted.csv") %>%
  pivot_longer(-country, names_to = "year", values_to = "GDP") %>% 
  filter(!is.na(GDP)) %>%
  arrange(year, GDP) %>%
  group_by(year) %>%
  summarise(GDP_over_all_countries = mean(GDP)) %>% 
  ungroup()
## 
## -- Column specification ----------------------------------------------------
## cols(
##   .default = col_double(),
##   country = col_character()
## )
## i Use `spec()` for the full column specifications.

Our aim is to analyze how the GDP has developed over time. The nice thing about plots is that we can use the whole range of years and still identify differences between various periods. Therefore, our plot of choice is a line plot to visualize the data as a time series.

1

Plot the Gapminder GDP per capita data as a line plot to display a time series.
Instead of geom_point as in the slides, the name of the geom we need is geom_line. In addition, in the aesthetics definition aes() you should define a grouping variable group = 1. Otherwise, ggplot assumes that you want to plot one line for each year.
ggplot(
  data = gapminder_ggplot_input,
  aes(x = year, y = GDP_over_all_countries, group = 1)
) +
  geom_line()

Admittedly, this may not be the best approach to identify differences between the periods directly. We don’t know when our periods start and when they end. Luckily, this can be done in two relatively straightforward steps. Let’s start with the first one: using different colors for different periods. For this purpose, we need an indicator variable as a grouping variable to use different colors for the line at each period.

2

Create an indicator variable for the periods 1960-1969, 2002-2018 and the time in between.
A combination of mutate() and case_when() lets you create the new variables we need. To get some sensible legend labels later, you should specify the indicator variables as strings.
gapminder_ggplot_input <-
  gapminder_ggplot_input %>% 
  mutate(
    period = 
      case_when(
        year >= 1960 & year <= 1969 ~ "1960-1969",
        year >= 1970 & year <= 2001 ~ "1970-2001",
        year >= 2002 & year <= 2018 ~ "2002-2018"
      )
  )

After we’re set up with our indicator variable, it’s plotting time again. We can simply reuse our code from before and define a grouping color in the aesthetics definition.

3

Plot the line plot once again, but this time with different colors for the different periods.
In the aesthetics definition aes(), you can choose the option color = indicator_variable to define the grouping.
ggplot(
  data = gapminder_ggplot_input,
  aes(
    x = year, 
    y = GDP_over_all_countries, 
    color = period, 
    group = 1
  )
) +
  geom_line()

Now we can see some visual differences between the different periods. One last thing, however, is that there are way too many labels on the x-axis. Maybe a more sensible axis labeling approach would be to create axis breaks for every ten years steps. However, this is an advanced exercise as we did not talk about manipulating axes before. If you’re not feeling adventurous just jump to the next exercise which is also optional but probably more exciting.

Bonus

Create some prettier, i.e., more sensible breaks for the x-axis.
You can modify the x-axis with scale_x_discrete() and its breaks with the option breaks = breaks_vector.
ggplot(
  data = gapminder_ggplot_input,
  aes(
    x = year, 
    y = GDP_over_all_countries, 
    color = period, 
    group = 1
  )
) +
  geom_line() +
  scale_x_discrete(
    breaks = seq(
      from = 1960, 
      to = 2011,
      by = 10
    )
  )